package com.icinfo.cloud.provider.common.crontask;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.utils.HeaderUtil;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigCasePunishBasisSyncMapper;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigCaseReasonSyncMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigCasePunishBasisSync;
import com.icinfo.cloud.provider.punish.config.model.ConfigCaseReasonSync;
import com.icinfo.framework.tools.utils.MapUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DataSyncTask {
    @Autowired
    private ConfigCaseReasonSyncMapper configCaseReasonSyncMapper;
    @Autowired
    private ConfigCasePunishBasisSyncMapper configCasePunishBasisSyncMapper;

    //@Scheduled(cron = "0 25 11 ? * *")
    public void ConfigCaseReasonSync(){
        String paramName="orgCode";
        String paramValue="331000000000_113310000026639221";
        String result=sendGet("https://zjzfpt.zjzwfw.gov.cn/api/v2/regulatory/auditPunishBasis/getAuditPubishTreeByOrgCode",paramName,paramValue);
        Map resultMap = (Map) JSONObject.parse(result);
        List<Map> dataList=(List<Map>)resultMap.get("data");
        for (Map resultDataMap:dataList) {
            ConfigCaseReasonSync configCaseReasonSync=new ConfigCaseReasonSync();
            configCaseReasonSync.setNodeId(MapUtils.getString(resultDataMap,"nodeId",""));
            configCaseReasonSync.setNodeCode(MapUtils.getString(resultDataMap,"nodeCode",""));
            configCaseReasonSync.setNodeName(MapUtils.getString(resultDataMap,"nodeName",""));
            configCaseReasonSync.setNodeSort(Integer.parseInt(MapUtils.getString(resultDataMap,"nodesort","")));
            configCaseReasonSync.setNodePid(MapUtils.getString(resultDataMap,"nodePid",""));
            configCaseReasonSync.setItemVersionCode(MapUtils.getString(resultDataMap,"itemVersionCode",""));
            configCaseReasonSync.setTerritoryCode(MapUtils.getString(resultDataMap,"territoryCode",""));
            configCaseReasonSync.setTerritoryName(MapUtils.getString(resultDataMap,"territoryName",""));
            configCaseReasonSync.setClaimId(MapUtils.getString(resultDataMap,"claimId",""));
            configCaseReasonSyncMapper.insert(configCaseReasonSync);
            List<Map>  childrensList=(List<Map>)resultDataMap.get("childrens");
            for (Map childrenMap:childrensList) {
                ConfigCaseReasonSync childrenConfigCaseReasonSync=new ConfigCaseReasonSync();
                configCaseReasonSync.setNodeId(MapUtils.getString(childrenMap,"nodeId",""));
                configCaseReasonSync.setNodeCode(MapUtils.getString(childrenMap,"nodeCode",""));
                configCaseReasonSync.setNodeName(MapUtils.getString(childrenMap,"nodeName",""));
                configCaseReasonSync.setNodeSort(Integer.parseInt(MapUtils.getString(childrenMap,"nodesort","")));
                configCaseReasonSync.setNodePid(MapUtils.getString(childrenMap,"nodePid",""));
                configCaseReasonSync.setItemVersionCode(MapUtils.getString(childrenMap,"itemVersionCode",""));
                configCaseReasonSync.setTerritoryCode(MapUtils.getString(childrenMap,"territoryCode",""));
                configCaseReasonSync.setTerritoryName(MapUtils.getString(childrenMap,"territoryName",""));
                configCaseReasonSync.setClaimId(MapUtils.getString(childrenMap,"claimId",""));
                configCaseReasonSyncMapper.insert(configCaseReasonSync);
            }
        }
    }

    //@Scheduled(cron = "0 6 14 ? * *")
    public void configCasePunishbasisSync(){
        List<ConfigCaseReasonSync> list=configCaseReasonSyncMapper.selectAll();
        for (ConfigCaseReasonSync configCaseReasonSync:list) {
            String nodeid=configCaseReasonSync.getNodeId();
            String result=sendGet("https://zjzfpt.zjzwfw.gov.cn/api/v2/regulatory/auditPunishBasis/findPubishBasisByBusId","busId",nodeid);
            Map resultMap = (Map) JSONObject.parse(result);
            if(null!=resultMap.get("data")){
                Map dataMap=(Map) resultMap.get("data");
                ConfigCasePunishBasisSync configCasePunishbasisSync=new ConfigCasePunishBasisSync();
                configCasePunishbasisSync.setId(MapUtils.getString(dataMap,"punishGuid",""));
                configCasePunishbasisSync.setBusId(nodeid);
                configCasePunishbasisSync.setIllegalBasis(MapUtils.getString(dataMap,"illegalBasis",""));
                configCasePunishbasisSync.setIllegalDetail(MapUtils.getString(dataMap,"illegalDetail",""));
                configCasePunishbasisSync.setPunishBasis(MapUtils.getString(dataMap,"punishBasis",""));
                configCasePunishbasisSync.setPunishDetail(MapUtils.getString(dataMap,"punishDetail",""));
                configCasePunishBasisSyncMapper.insert(configCasePunishbasisSync);
            }

        }

    }

    public static String sendGet(String url,String paramName,String paramValue) {
        String result="";
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            client = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder(url);
            //uriBuilder.addParameter("orgCode","331000000000_113310000026639221");
            uriBuilder.addParameter(paramName,paramValue);
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            httpGet.addHeader("Content-Type", "application/json");
            httpGet.addHeader("Authorization","Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJsaXlhZmVuZyIsImF1ZCI6IndlYiIsImNyZWF0ZWQiOjE2NDg2Mjg3NTE5NjMsImFwcElkIjpudWxsLCJpc3MiOiJpY2luZm8uY24iLCJleHAiOjE2NDkyMzM1NTF9.hAskenWteG6GqzHugwXjGrYrIRnfQGbDKxy4SmE3VpNVVDnyJyLz9p73hGoYWWG2oYIUEe-rcexAR_mWsE184Q");
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "UTF-8");
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
